﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using GestionPedago.EntityContext;

namespace Persistence.OPManager
{
    /// <summary>
    /// Manager de persistance de l'Entity Entreprise
    /// </summary>
    public abstract class EntrepriseOPManager
    {
        /// <summary>
        /// Méthode de récupération de la liste d'entreprises
        /// </summary>
        /// <param name="filter">Le filtre de recherche</param>
        /// <param name="orderByRaisonSocialeAsc">L'ordre de tri par raison social (vrai = ascendant)</param>
        /// <returns>La liste d'entreprises</returns>
        public static List<Entreprise> GetListeEntreprise(string filter = "", bool orderByRaisonSocialeAsc = true)
        {
            try
            {
                IQueryable<Entreprise> query = EntityConnection.GetContext().Entreprise;
                if (!String.IsNullOrWhiteSpace(filter))
                {
                    query = query.Where(it => it.RaisonSociale.Contains(filter));
                }
                if (orderByRaisonSocialeAsc)
                {
                    query = query.OrderBy(it => it.RaisonSociale);
                }
                else
                {
                    query = query.OrderByDescending(it => it.RaisonSociale);
                }
                /* query.Select(it => it);*/

                List<Entreprise> lst = new List<Entreprise>();

                List<Entreprise> listeEntreprise = query.ToList();
                foreach(Entreprise t in listeEntreprise)
                {

                    lst.Add(t);
                }

                return lst;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Méthode de récupération d'une entreprise par son code
        /// </summary>
        /// <param name="codeEntreprise">L'identifiant de l'entreprise</param>
        /// <returns>L'entreprise</returns>
        public static Entreprise GetEntrepriseByCode(int codeEntreprise)
        {

            try
            {
                var query = from it in EntityConnection.GetContext().Entreprise
                            where it.CodeEntreprise == codeEntreprise
                            select it;

                Entreprise e = null;

                e = query.First();

                return e;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}